package com.hejia.bulbul.controller;

import java.util.Date;
import java.util.HashMap;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSONObject;
import com.hejia.alauda.entity.Parameter;
import com.hejia.alauda.entity.Warning;
import com.hejia.alauda.entity.WarningSetting;
import com.hejia.alauda.entity.example.WarningExample;
import com.hejia.alauda.service.ConfigureService;
import com.hejia.alauda.utils.Pager;
import com.hejia.bulbul.entity.DataTablesReturnObject;
import com.hejia.bulbul.entity.JSONParam;

@Controller
@RequestMapping("warning")
public class WarningController extends BaseController {

	@Resource
	private ConfigureService warningService;

	@Resource
	private ConfigureService parameterService;

	/**
	 * 列表
	 */
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String warningList(Pager<Warning> pager, ModelMap model) {
		Pager<Warning> warning = warningService.findWarning(pager);
		Parameter parameter = parameterService.getParameter(
				Parameter.Category.Warning, "Setting");

		WarningSetting warningSetting = JSONObject.parseObject(parameter.getValue(), WarningSetting.class);
		model.put("parameter", warningSetting);
		model.put("warning", warning);
		return "/setting/warning_list";
	}

	/**
	 * 添加完保存
	 */
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String saveVersionList(Warning warning, ModelMap model,
			RedirectAttributes redirectAttributes) {

		warning.setCreateDate(new Date());
		if (warningService.saveWarning(warning)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "预警添加成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "预警添加失败");
		}

		return "redirect:/warning/list";
	}

	/**
	 * 参数保存
	 * 
	 * @throws Exception
	 */
	@RequestMapping(value = "save", method = RequestMethod.POST)
	public String saveParam(WarningSetting warningSetting, ModelMap model,
			RedirectAttributes redirectAttributes) throws Exception {

		if (warningService.saveParam(warningSetting)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "预警参数保存成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "预警参数保存失败");
		}

		return "redirect:/warning/list";
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
	public String deleteWarning(@PathVariable Long id,
			RedirectAttributes redirectAttributes) {
		if (warningService.deleteWarning(id)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "预警删除成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "预警删除失败");
		}

		return "redirect:/warning/list";
	}

	/**
	 * 以ajax格式返回列表请求
	 *
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "warningAsAjax", method = RequestMethod.POST)
	@ResponseBody
	public DataTablesReturnObject<Warning> warningAsAjax(
			@RequestBody JSONParam[] params) {
		HashMap<String, String> paramMap = JSONParam.convertToMap(params);

		// 分页信息
		Pager<Warning> pager = new Pager<Warning>();
		int start = Integer.parseInt(paramMap.get("iDisplayStart"));
		int length = Integer.parseInt(paramMap.get("iDisplayLength"));
		pager.setPageSize(length);
		pager.setPageNumber(start / length + 1);

		// 查询条件
		WarningExample exp = new WarningExample();

		String sSearch = paramMap.get("sSearch");
		if (StringUtils.isNotEmpty(sSearch)) {
			exp.appendCriterion("phoneNum like  ", "%" + sSearch + "%");
		}
		// 排序
		int iSortingCols = Integer.parseInt(paramMap.get("iSortingCols"));
		for (int i = 0; i < iSortingCols; i++) {
			String iSortCol = paramMap.get("iSortCol_" + i);
			String mDataProp = paramMap.get("mDataProp_" + iSortCol);
			String sSortDir = paramMap.get("sSortDir_" + i);
			if (i > 0) {
				exp.setOrderByClause(exp.getOrderByClause() + ",");
				exp.setOrderByClause(exp.getOrderByClause() + mDataProp + " "
						+ sSortDir);
			} else {
				exp.setOrderByClause(mDataProp + " " + sSortDir);
			}
		}

		pager = warningService.findWarning(pager, exp);

		DataTablesReturnObject<Warning> dtr = new DataTablesReturnObject<Warning>(
				pager.getTotalCount(), paramMap.get("sEcho"), pager.getList());
		return dtr;
	}

}
